<template>
  <div>
    <el-input
        placeholder="输入关键字进行搜索..."
        style="width: 600px"
        v-model="filterText">
    </el-input>

    <el-tree
        class="filter-tree"
        style="width: 600px"
        :data="depts"
        :props="defaultProps"
        :filter-node-method="filterNode"
        :expand-on-click-node="false"
        ref="tree">
      <template #default="{ node, data }">
        <span class="custom-tree-node">
          <span>{{ data.name }}</span>
          <span>
            <el-button size="mini"
                       class="deptBtn"
                       type="success"
                       @click="() => showAddDept(data)">添加部门</el-button>
           <el-button size="mini"
                      class="deptBtn"
                      type="warning"
                      @click="() => deleteDept(data)">删除部门</el-button>
          </span>
        </span>
      </template>
    </el-tree>
    <el-dialog
        title="添加部门"
        :visible.sync="dialogVisible"
        width="30%">
      <div>
        <table>
          <tr>
            <td>
              <el-tag>上级部门</el-tag>
            </td>
            <td>{{parentName}}</td>
          </tr>
          <tr>
            <td>
              <el-tag>部门名称</el-tag>
            </td>
            <td>
              <el-input v-model="dept.name" placeholder="请输入部门名称..."></el-input>
            </td>
          </tr>
        </table>
      </div>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="addDept" size="small">确 定</el-button>
      </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "DepManage",
  data() {
    return {
      filterText: '',
      depts: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      dept: {
        name: '',
        parentId: -1
      },
      parentName: '',
      dialogVisible: false
    }
  },

  watch: {
    filterText(val) {
      this.$refs.tree.filter(val);
    }
  },

  mounted() {
    this.initDept();
  },

  methods: {
    initDep() {
      this.dept = {
        name: '',
        parentId: -1
      };
      this.parentName = '';
    },
    addDep2Depts(depts, dep) {
      for (let i = 0; i < depts.length; i++) {
        let d = depts[i];
        if (d.id == dep.parentId) {
          d.children = d.children.concat(dep);
          if (d.children.length > 0) {
            d.isParent = true;
          }
          return ;
        } else {
          this.addDep2Depts(d.children, dep);
        }
      }
    },
    addDept() {
      this.postRequest('/system/basic/department/', this.dept).then(resp => {
        if (resp) {
          // this.addDep2Depts(this.depts, resp.obj);
          this.initDept();
          this.dialogVisible = false;
          this.initDep();
        }
      });
    },
    showAddDept(data) {
      this.dept.parentId = data.id;
      this.parentName = data.name;
      this.dialogVisible = true;
    },
    delDep2Depts(p,depts, dep) {
      for (let i = 0; i < depts.length; i++) {
        let d = depts[i];
        if (d.id == dep.id) {
          //从索引i位置开始删除一个
          depts.splice(i,1);
          if (depts.length == 0) {
            p.isParent = false;
          }
        } else {
          this.delDep2Depts(d,d.children, dep);
        }
      }
    },
    deleteDept(data) {
      console.log(data);
      if (!data.isParent) {
        this.$confirm('此操作将永久删除'+data.name+'部门, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.deleteRequest('/system/basic/department/'+data.id).then(resp => {
            this.delDep2Depts(null, this.depts, data);
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      } else {
        this.$message({
          type: 'error',
          message: '该部门有子部门不能删除！'
        });
      }
    },
    filterNode(value, data) {
      if (!value) return true;
      return data.name.indexOf(value) !== -1;
    },
    initDept() {
      this.getRequest('/system/basic/department/').then(resp => {
        if (resp) {
          this.depts = resp.obj;
        }
      });
    }
  },
}
</script>

<style scoped>
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
.deptBtn {
  padding: 1px;
}
</style>